<template>
  <!-- 放置弹层组件 -->
  <el-dialog title="新增部门" :visible="showDialog" @close="closeDialog" @open="openDialog">
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in employeeList"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" size="small" @click.native="clickSubmit">确定</el-button>
      <el-button size="small" @click.native="closeDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employee'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'

export default {
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    curId: {
      type: String,
      default: ''
    },
    allList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    const validateCode = (rule, value, callback) => {
      // 1. 拿到所有的原始节点数据  allList
      // 2. allList里面每一项的code码拿到组成一个新数组
      const codeList = this.allList.map(item => item.code)
      // 把正在编辑项的 code 码  筛选出去  保留其余的 code 码
      let _codelist = []
      if (this.form.id) {
        _codelist = codeList.filter(item => item !== this.cacheCode)
      } else {
        _codelist = codeList
      }
      if (_codelist.includes(value)) {
        return callback(new Error('code不能重复'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: 'blur', message: 'code码重复' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      employeeList: [],
      cacheCode: '' // 缓存 编辑项 的 code 码
    }
  },
  methods: {
    async openDialog() {
      const res = await getEmployeeSimple()
      this.employeeList = res
    },
    closeDialog() {
      this.$emit('close-dialog')
      // 初始化 表单数据
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      // 清除校验痕迹
      this.$refs.form.resetFields()
    },
    clickSubmit() {
      this.$refs.form.validate(async(bool) => {
        if (bool) {
          if (this.form.id) {
            await updateDepartments(this.form)
          } else {
            // 调用接口 传递数据
            await addDepartments({ ...this.form, pid: this.curId })
          }
          //  通知父级 刷新列表
          this.$emit('update-list')
          //  关闭弹窗
          this.closeDialog()
        }
      })
    },
    async getDetail(id) {
      const res = await getDepartDetail(id)
      this.form = res
      // 缓存  正在编辑项的 code 码  为之后的判断做准备
      this.cacheCode = res.code
    }
  }
}
</script>
